const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const app = getApp()

Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    state: Boolean
  },
  data: {
    // 这里是一些组件内部数据
    offsetH: 0,
    level:0,
    levelStars:0,
    levelPre:0,
    uId:'',
    curClass:'',
    
    qualifyingNames:[]
  },
  methods: {
    switchFn() {
      this.triggerEvent('myevent', { state: !this.data.state });
    },

    getPersonageLevel(uId) {
      qcloud.request({
        url: config.service.obtainQualifyingUpgradeInfo,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        login: true,
        data: {
          'uId': uId
        },
        success: (response) => {
          let data = response.data
          let iarr = ['zero', 'one', 'two', 'three']

          if (data.code === 0) {
            this.setData({
              level: Number(data.upgradeInfo['level'])
            })
            this.setData({
              levelStars: Number(data.upgradeInfo['levelStars'])
            })
            this.setData({
              levelPre: Number(data.upgradeInfo['levelPre'])
            })
            
            this.setData({
              curClass: iarr[this.data.levelStars > 3 ? 3 : this.data.levelStars]
            })

            if (this.data.levelPre < this.data.level) {
              this.triggerEvent('updateStatus', { state: true, name: this.data.qualifyingNames[this.data.level] })
            }
          }
        },
        fail: function (err) {
          console.log(err);
        }
      })
    }
  },


  attached() {
    this.setData({
      qualifyingNames: app.globalData.qualifyingNames
    })

    this.setData({
      uId: app.globalData.uId
    })

    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          offsetH: res.windowHeight
        })
      }
    })

    this.getPersonageLevel(this.data.uId)
  }

  
})